package fr.ydelouis.swipelistviewsample.helper;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.ConnectivityManager;
import android.net.Uri;
import android.provider.ContactsContract.PhoneLookup;

public class Utinities {

	public static String getContactName(Context context, String phoneNumber) {
		ContentResolver cr = context.getContentResolver();
		Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
				Uri.encode(phoneNumber));
		Cursor cursor = cr.query(uri,
				new String[] { PhoneLookup.DISPLAY_NAME }, null, null, null);
		if (cursor == null) {
			return null;
		}

		String contactName = phoneNumber;
		try {
			if (cursor.moveToFirst()) {
				contactName = cursor.getString(cursor
						.getColumnIndex(PhoneLookup.DISPLAY_NAME));
			}
			if (cursor != null && !cursor.isClosed()) {
				cursor.close();
			}
		} catch (Exception ex) {
		}
		return contactName;
	}

	public static int getDateDiffString(Date dateOne, Date dateTwo) {
		SimpleDateFormat dfDate = new SimpleDateFormat("dd/MM/yyyy");

		try {
			dateOne = dfDate.parse(dfDate.format(dateOne));
			dateTwo = dfDate.parse(dfDate.format(dateTwo));// Returns
															// 15/10/2012
		} catch (java.text.ParseException e) {
			e.printStackTrace();
		}

		int diffInDays = (int) ((dateTwo.getTime() - dateOne.getTime()) / (1000 * 60 * 60 * 24));

		return diffInDays;
	}

	public static boolean checkInternetConnection(Context context) {
		ConnectivityManager cm = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		// test for connection
		if (cm.getActiveNetworkInfo() != null
				&& cm.getActiveNetworkInfo().isAvailable()
				&& cm.getActiveNetworkInfo().isConnected()) {
			return true;
		} else {

			return false;
		}
	}
}
